/**
 * MIME类型与后缀互查
 * 		typ!=null时:后缀查MIME类型,特别的:'*'=>'application/octet-stream'
 * @param val string 查询的MIME类型或后缀  MIME:不识别';'  后缀:不含'.'
 * @param typ string|null 方式及默认的后缀  null:MIME类型查后缀 '':无默认
 * @param enc string 仅typ!=null有效  文本类型时追加编码  '':不追加
 * @return string '':未找到(包括typ!=null)
 */
$.mime = function(val, typ=null, enc='utf-8'){
	const MIME = [
		['txt','text/plain'], ['html','text/html'],['htm','text/html'],
		['css','text/css'], ['js','application/javascript'],
		['mjs','application/javascript'], ['json','application/json'],
		['xml','application/xml'], ['swf','application/x-shockwave-flash'],
		// images
		['png','image/png'], ['jpg','image/jpeg'], ['jpe','image/jpeg'],
		['jpeg','image/jpeg'], ['gif','image/gif'], ['bmp','image/bmp'],
		['ico','image/x-icon'], ['tif','image/tiff'], ['tiff','image/tiff'],
		['svg','image/svg+xml'], ['svgz','image/svg+xml'],
		['webp','image/webp'], ['wbmp','image/vnd.wap.wbmp'],
		// archives
		['zip','application/zip'], ['rar','application/x-rar-compressed'],
		['exe','application/x-msdownload'], ['msi','application/x-msdownload'],
		['cab','application/vnd.ms-cab-compressed'],
		// audio
		['mp3','audio/mpeg'], ['mid','audio/midi'], ['midi','audio/midi'],
		['kar','audio/midi'], ['ogg','audio/ogg'], ['m4a','audio/x-m4a'],
		['ra','audio/x-realaudio'],
		// video
		['qt','video/quicktime'], ['mov','video/quicktime'], ['3gp','video/3gpp'],
		['3gpp','video/3gpp'], ['ts','video/mp2t'], ['mp4','video/mp4'],
		['mpg','video/mpeg'], ['mpeg','video/mpeg'], ['webm','video/webm'],
		['flv','video/x-flv'], ['m4v','video/x-m4v'], ['mng','video/x-mng'],
		['asf','video/x-ms-asf'], ['asx','video/x-ms-asf'],
		['wmv','video/x-ms-wmv'], ['avi','video/x-msvideo'],
		// adobe
		['pdf','application/pdf'], ['psd','image/vnd.adobe.photoshop'],
		['ai','application/postscript'], ['eps','application/postscript'],
		['ps','application/postscript'],
		// MS Office
		['doc','application/msword'], ['rtf','application/rtf'],
		['xls','application/vnd.ms-excel'],
		['ppt','application/vnd.ms-powerpoint'],
		// open office
		['odt','application/vnd.oasis.opendocument.text'],
		['ods','application/vnd.oasis.opendocument.spreadsheet'],
	];
	const TXT = ['javascript', 'json', 'xml'];
	const EXT = v=>v==='*'?'application/octet-stream':GET(v,1);
	const GET = function (v, x){
		let i=0, n=x?0:1, len=MIME.length;  v=v.toLowerCase();
		for(; i<len; i++) if(MIME[i][n]===v) return MIME[i][x];
		return '';
	};
	$.mime = function(val, typ=null, enc='utf-8'){
		if(typ===null) return GET(val,0); let v=EXT(val);
		if(!v && typ) v=EXT(typ);  if(!v||!enc) return v;
		let [p,x]=v.split('/');  p=p!=='text';
		return p&&TXT.indexOf(x)<0?v:v+'; charset='+enc;
	};
	return $.mime(val, typ, enc);
};
